//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.mega.ycy.common.specialattack;

import com.mega.ycy.Decide;
import com.mega.ycy.Yiciyuan_EVENT;
import mods.flammpfeil.slashblade.item.ItemSlashBlade;
import mods.flammpfeil.slashblade.item.ItemSlashBlade.ComboSequence;
import mods.flammpfeil.slashblade.specialattack.SpecialAttackBase;
import net.minecraft.client.Minecraft;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.SoundEvents;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.EnumParticleTypes;
import net.minecraft.util.math.AxisAlignedBB;
import net.minecraft.util.math.Vec3d;
import net.minecraft.world.World;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class Delete extends SpecialAttackBase {
    public Delete() {
    }

    public static Entity getEntityToWatch(EntityPlayer player) {
        Entity target = null;

        for(int dist = 2; dist < 60; dist += 2) {
            AxisAlignedBB bb = player.getEntityBoundingBox();
            Vec3d vec = player.getLookVec();
            vec = vec.normalize();
            bb = bb.grow(15.0D, 15.0D, 15.0D);
            bb = bb.offset(vec.x * (double)dist, vec.y * (double)dist, vec.z * (double)dist);
            List<Entity> list = new ArrayList<>();
            List<Entity> list1 = player.world.getEntitiesWithinAABBExcludingEntity(player, bb);
            if (!list1.isEmpty()) {
                for(int i = 0; i < list1.size() - 1 && list1.get(i) != null; ++i) {
                    Entity entity = list1.get(i);
                    if (entity != null && !entity.isDead) {
                        list.add(entity);
                    }
                }
            }

            float distance = 50.0F;

            for(int i = 0; i < list.size() - 1 && list.get(i) != null; ++i) {
                Entity curEntity = list.get(i);
                float curDist = curEntity.getDistance(player);
                if (curDist < distance) {
                    target = curEntity;
                    distance = curDist;
                }
            }

            if (target != null) {
                break;
            }
        }

        return target;
    }

    public String toString() {
        return "delete";
    }

    public void doSpacialAttack(ItemStack stack, EntityPlayer player) {
        World world = player.world;
        this.spawnParticle(world, player);
        NBTTagCompound tag = ItemSlashBlade.getItemTagCompound(stack);
        Entity look = getEntityToWatch(player);

        for(int i = 0; i < 2; ++i) {
            double var10004;
            double var10005;
            double var10006;
            if (look != null) {
                Iterator<Entity> var16 = look.world.getEntitiesWithinAABBExcludingEntity(look, new AxisAlignedBB(look.posX - 20.0D, look.posY - 20.0D, look.posZ - 20.0D, look.posX + 20.0D, look.posY + 20.0D, look.posZ + 20.0D)).iterator();

                Entity entity;
                while(var16.hasNext()) {
                    entity = var16.next();
                    Yiciyuan_EVENT.killEntity(entity);
                }

                var10004 = look.posX - 20.0D;
                var10005 = look.posY - 20.0D;
                var10006 = look.posZ - 20.0D;
                var16 = Minecraft.getMinecraft().world.getEntitiesWithinAABBExcludingEntity(look, new AxisAlignedBB(var10004, var10005, var10006, look.posX + 20.0D, look.posY + 20.0D, look.posZ + 20.0D)).iterator();

                while(var16.hasNext()) {
                    entity = var16.next();
                    Yiciyuan_EVENT.killEntity(entity);
                }

                if (Minecraft.getMinecraft().getIntegratedServer() != null) {
                    var16 = Minecraft.getMinecraft().getIntegratedServer().getEntityWorld().getEntitiesWithinAABBExcludingEntity(look, new AxisAlignedBB(look.posX - 20.0D, look.posY - 20.0D, look.posZ - 20.0D, look.posX + 20.0D, look.posY + 20.0D, look.posZ + 20.0D)).iterator();

                    while(var16.hasNext()) {
                        entity = var16.next();
                        Yiciyuan_EVENT.killEntity(entity);
                    }
                }
            } else {
                Vec3d vec3d = player.getLookVec().scale(30.0D);
                double x = vec3d.x;
                double y = vec3d.y;
                double z = vec3d.z;
                Iterator<Entity> var14 = player.world.getEntitiesWithinAABBExcludingEntity(player, new AxisAlignedBB(player.posX - 20.0D + x, player.posY - 20.0D + y, player.posZ - 20.0D + z, player.posX + 20.0D + x, player.posY + 20.0D + y, player.posZ + 20.0D + z)).iterator();
                Iterator<Entity> entityIterator = Yiciyuan_EVENT.RENDERABLE_ENTITIES.iterator();

                Entity entity;
                while(var14.hasNext()) {
                    entity = var14.next();
                    Yiciyuan_EVENT.killEntity(entity);
                }

                Entity entity2;
                    while (entityIterator.hasNext()) {
                        entity2 = entityIterator.next();
                        Yiciyuan_EVENT.killEntity(entity2);
                    }
                var10004 = player.posX - 20.0D + x;
                var10005 = player.posY - 20.0D + y;
                var10006 = player.posZ - 20.0D + z;
                double var10007 = player.posX + 20.0D + x;
                var14 = Minecraft.getMinecraft().world.getEntitiesWithinAABBExcludingEntity(player, new AxisAlignedBB(var10004, var10005, var10006, var10007, player.posY + 20.0D + y, player.posZ + 20.0D + z)).iterator();

                while(var14.hasNext()) {
                    entity = var14.next();
                    Yiciyuan_EVENT.killEntity(entity);
                }

                if (Minecraft.getMinecraft().getIntegratedServer() != null) {
                    var14 = Minecraft.getMinecraft().getIntegratedServer().getEntityWorld().getEntitiesWithinAABBExcludingEntity(player, new AxisAlignedBB(player.posX - 20.0D + x, player.posY - 20.0D + y, player.posZ - 20.0D + z, player.posX + 20.0D + x, player.posY + 20.0D + y, player.posZ + 20.0D + z)).iterator();

                    while(var14.hasNext()) {
                        entity = var14.next();
                        Yiciyuan_EVENT.killEntity(entity);
                    }
                }
            }
        }
        Decide.removeChaosWither();
        ItemSlashBlade.setComboSequence(tag, ComboSequence.SlashDim);
        player.playSound(SoundEvents.ENTITY_WITHER_HURT, 0.5F, 1.0F);
    }

    public static void spawnParticle(World world, Entity target) {
        world.spawnParticle(EnumParticleTypes.EXPLOSION_LARGE, target.posX, target.posY + (double)target.height, target.posZ, 3.0D, 3.0D, 3.0D);
        world.spawnParticle(EnumParticleTypes.EXPLOSION_LARGE, target.posX + 1.0D, target.posY + (double)target.height + 1.0D, target.posZ, 3.0D, 3.0D, 3.0D);
        world.spawnParticle(EnumParticleTypes.EXPLOSION_LARGE, target.posX, target.posY + (double)target.height + 0.5D, target.posZ + 1.0D, 3.0D, 3.0D, 3.0D);
    }
}
